

#ifndef RAYCI_BEAM_Quality_RESULT_H
#define RAYCI_BEAM_Quality_RESULT_H

#include <QWidget>
#include <QTreeWidgetItem>
#include "ui_Beam_Quality_Result.h"
#include "BeamQualityTypes.h"


#define COLUMN_PARAMETER         0  // 参数名称列
#define COLUMN_SYMBOL            1  // 符号列
#define COLUMN_VALUE             2  // 值列
#define COLUMN_UNCERTAINTY_SYMBOL 3  // 不确定性符号列
#define COLUMN_UNCERTAINTY_VALUE  4  // 不确定性值列
#define COLUMN_UNIT              5  // 单位列


QT_BEGIN_NAMESPACE
namespace Ui { class Beam_Quality_Result;; }
QT_END_NAMESPACE

class Beam_Quality_Result : public QWidget {
    Q_OBJECT

    public:
    explicit Beam_Quality_Result(QWidget *parent = nullptr);

    ~Beam_Quality_Result() override;

    void clear();

    void updateCurrentPlane(const CurrentPlaneParams& params);

    // 使用结构体的更新函数
    void updateResult(const bool ALLUpdate,
        const CurrentPlaneParams& currentPlane,
        const GeneralParams& general,
        const TorsionParams& torsion,
        const UCausticParams& uCaustic,
        const VCausticParams& vCaustic,
        const UVCausticParams& uvCaustic,
        const CausticFitParams& causticFit,
        const Caustic3DFitParams& caustic3DFit
        );

    // 使用单一结构体的更新函数
    void updateResult(bool update,const BeamQualityResultParams& params);

    // 更新带不确定性的项目
    inline void updateItemWithUncertainty(
        QTreeWidgetItem* item,
        const QString& value,
        const QString& uncertainty
    );

    bool isDouble(const QString &str);
    void showISO_1();
    void showISO_2();
    void showDetails();
    void SetIsRawResult(bool isRawBeam);


private:

    void updateCausticParams(
        QTreeWidgetItem* isoItem,
        QTreeWidgetItem* positionItem,
        QTreeWidgetItem* waistItem,
        QTreeWidgetItem* rayleighItem,
        QTreeWidgetItem* divergenceItem,
        QTreeWidgetItem* bppItem,
        QTreeWidgetItem* ratioItem,
        QTreeWidgetItem* factorItem,
        const CausticParamsBase& params
    );

    Ui::Beam_Quality_Result *ui;
    QTreeWidgetItem* oneTopItem = nullptr;
    QTreeWidgetItem* oneTop = nullptr;
    QTreeWidgetItem* NumberItem = nullptr;
    QTreeWidgetItem* CentroidXItem = nullptr;
    QTreeWidgetItem* CentroidYItem = nullptr;
    QTreeWidgetItem* PositionZItem = nullptr;
    QTreeWidgetItem* WidthGeneralizedItem = nullptr;
    QTreeWidgetItem* WidthMajorItem = nullptr;
    QTreeWidgetItem* WidthMinorItem = nullptr;
    QTreeWidgetItem* AzimuthAngleItem = nullptr;
    QTreeWidgetItem* EllipticityItem = nullptr;
    QTreeWidgetItem* EccentricityItem = nullptr;


    QTreeWidgetItem* twoTopItem = nullptr;
    QTreeWidgetItem* twoTop = nullptr;
    QTreeWidgetItem* NumberofPositionsItem = nullptr;
    QTreeWidgetItem* GeometricBeamTypeItem = nullptr;
    QTreeWidgetItem* ResultsItem = nullptr;

    QTreeWidgetItem* threeTopItem = nullptr;
    QTreeWidgetItem* threeTop = nullptr;
    QTreeWidgetItem* N_ellipticalItem = nullptr;
    QTreeWidgetItem* TorsionItem = nullptr;

    QTreeWidgetItem* fourTopItem = nullptr;
    QTreeWidgetItem* fourTop = nullptr;
    QTreeWidgetItem* uISO11146_1Item = nullptr;
    QTreeWidgetItem* uCausticPositionItem = nullptr;
    QTreeWidgetItem* uBeamWaistDiameterItem = nullptr;
    QTreeWidgetItem* uRayleighLengthItem = nullptr;
    QTreeWidgetItem* uDivergenceItem = nullptr;
    QTreeWidgetItem* uBeamParameterProductItem = nullptr;
    QTreeWidgetItem* uBeamPropagationRatioItem = nullptr;
    QTreeWidgetItem* uBeamPropagationFactorItem = nullptr;


    QTreeWidgetItem* fiveTopItem = nullptr;
    QTreeWidgetItem* fiveTop = nullptr;
    QTreeWidgetItem* vISO11146_1Item = nullptr;
    QTreeWidgetItem* vCausticPositionItem = nullptr;
    QTreeWidgetItem* vBeamWaistDiameterItem = nullptr;
    QTreeWidgetItem* vRayleighLengthItem = nullptr;
    QTreeWidgetItem* vDivergenceItem = nullptr;
    QTreeWidgetItem* vBeamParameterProductItem = nullptr;
    QTreeWidgetItem* vBeamPropagationRatioItem = nullptr;
    QTreeWidgetItem* vBeamPropagationFactorItem = nullptr;

    QTreeWidgetItem* sixTopItem = nullptr;
    QTreeWidgetItem* sixTop = nullptr;
    QTreeWidgetItem* AstigmatismRelItem = nullptr;
    QTreeWidgetItem* AsymmetryItem = nullptr;

    QTreeWidgetItem* sevenTopItem = nullptr;
    QTreeWidgetItem* sevenTop = nullptr;
    QTreeWidgetItem* CausticPositionItem = nullptr;
    QTreeWidgetItem* BeamWaistDiameterItem = nullptr;
    QTreeWidgetItem* ReyleighLengthItem = nullptr;
    QTreeWidgetItem* DivergenceItem = nullptr;

    QTreeWidgetItem* eightTopItem = nullptr;
    QTreeWidgetItem* eightTop = nullptr;
    QTreeWidgetItem* ISO11146_2Item = nullptr;
    QTreeWidgetItem* ReyleighLengthleftItem = nullptr;
    QTreeWidgetItem* ReyleighLengthrightItem = nullptr;
    QTreeWidgetItem* TwistItem = nullptr;
    QTreeWidgetItem* AstigmatismrelItem = nullptr;
    QTreeWidgetItem* AstigmatismangleItem = nullptr;
    QTreeWidgetItem* Divergence3DItem = nullptr;
    QTreeWidgetItem* BeamParameterProductItem = nullptr;
    QTreeWidgetItem* BeamPropagationRatioItem = nullptr;
    QTreeWidgetItem* BeamPropagationFactorItem = nullptr;
    QTreeWidgetItem* IntrinsicAstigmatismItem = nullptr;

    QTreeWidgetItem* nineTopItem = nullptr;
    QTreeWidgetItem* nineTop = nullptr;
    QTreeWidgetItem* ISO11146_2_SummaryItem = nullptr;
    QTreeWidgetItem* CausticPosition_SummaryItem = nullptr;
    QTreeWidgetItem* BeamWaistDiameter_SummaryItem = nullptr;
    QTreeWidgetItem* ReyleighLength_SummaryItem = nullptr;
    QTreeWidgetItem* Divergence_SummaryItem = nullptr;
    QTreeWidgetItem* Twist_SummaryItem = nullptr;
    QTreeWidgetItem* Astigmatismrel_SummaryItem = nullptr;
    QTreeWidgetItem* Astigmatismangle_SummaryItem = nullptr;
    QTreeWidgetItem* Divergence3D_SummaryItem = nullptr;
    QTreeWidgetItem* BeamParameterProduct_SummaryItem = nullptr;
    QTreeWidgetItem* BeamPropagationRatio_SummaryItem = nullptr;
    QTreeWidgetItem* BeamPropagationFactor_SummaryItem = nullptr;
    QTreeWidgetItem* IntrinsicAstigmatism_SummaryItem = nullptr;

    template<typename T>
    struct TreeItemData {
        QTreeWidgetItem*& item;
        T data;
    };




    void setupTreeWidget();
    void createTopLevelSection(int index, const QString& title, QTreeWidgetItem*& topItem,
                             const std::vector<TreeItemData<QStringList>>& children);
    void addChildItems(QTreeWidgetItem* parent, const std::vector<TreeItemData<QStringList>>& children);
};


#endif //RAYCI_BEAM_Quality_RESULT_H
